﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace CSSAdriverapp
{
    /// <summary>
    /// Interaction logic for EditDriver.xaml
    /// </summary>
    public partial class EditDriver : Window
    {
        public EditDriver()
        {
            InitializeComponent();
            populateGrid();
        }

        public void populateGrid()
        {
            DataClassesDataContext db = new DataClassesDataContext();
            IQueryable<Driver> drivers = from c in db.Drivers select c;
            drivers.ToList();
            displaydataGrid.ItemsSource = drivers;
        }

        private void editDriverButton_Click(object sender, RoutedEventArgs e)//Edit Driver
        {
            if (displaydataGrid.SelectedItem != null)
            {
                DataClassesDataContext db = new DataClassesDataContext();
                // Query the database for the row to be updated.
                var query =
                    from c in db.Drivers
                    where c.ID == ((Driver)(displaydataGrid.SelectedItem)).ID
                    select c;

                // Execute the query, and change the column values
                // you want to change.
                foreach (Driver c in query)
                {
                    c.first_name = firstNametextBox.Text;
                    c.last_name = lastNametextBox.Text;
                    c.EMail = eMailtextBox.Text;
                    c.PhoneNumber = phonetextBox.Text;
                    // Insert any additional changes to column values.
                }

                // Submit the changes to the database.
                try
                {
                    db.SubmitChanges();
                    editSuccesfulLabel.Visibility = System.Windows.Visibility.Visible;
                }
                catch (Exception c)
                {
                    Console.WriteLine(c);
                    // Provide for exceptions.
                }

                populateGrid(); //Shows new values in grid

                string savedMessage = "Changes Saved";
                string editCaption = "Driver Edit";
                MessageBoxResult result1 = MessageBox.Show(savedMessage, editCaption, MessageBoxButton.OK, MessageBoxImage.Exclamation);
            }
            else
            {
                string selectMessage = "Please select a driver";
                string driverCaption = "Driver";
                MessageBoxResult result = MessageBox.Show(selectMessage, driverCaption,
                                         MessageBoxButton.OK,
                                         MessageBoxImage.Exclamation);
            }


        }
        public List<Driver> drivers
        {
            get;
            set;
        }

        private void displaydataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            editSuccesfulLabel.Visibility = System.Windows.Visibility.Hidden;
            if (displaydataGrid.SelectedItem != null)
            {
                firstNametextBox.Text = ((Driver)(displaydataGrid.SelectedItem)).first_name;
                lastNametextBox.Text = ((Driver)(displaydataGrid.SelectedItem)).last_name;
                eMailtextBox.Text = ((Driver)(displaydataGrid.SelectedItem)).EMail;
                phonetextBox.Text = ((Driver)(displaydataGrid.SelectedItem)).PhoneNumber;
            }
        }

        private void deleteButton_Click(object sender, RoutedEventArgs e)
        {
            if (displaydataGrid.SelectedItem != null)
            {
                //Message box code
                string message = "Are you sure? This action will delete this driver";
                string caption = "Confirmation";
                MessageBoxResult result = MessageBox.Show(message, caption,
                                         MessageBoxButton.YesNo,
                                         MessageBoxImage.Question);
                if (result == MessageBoxResult.Yes)
                {

                    try
                    {
                        DataClassesDataContext db = new DataClassesDataContext();
                        // Query the database for the rows to be deleted.
                        var deleteVA =
                            from details in db.Drivers
                            where details.ID == ((Driver)(displaydataGrid.SelectedItem)).ID
                            select details;

                        foreach (var detail in deleteVA)
                        {
                            db.Drivers.DeleteOnSubmit(detail);
                        }

                        try
                        {
                            db.SubmitChanges();
                            populateGrid();
                        }
                        catch (Exception c)
                        {
                            Console.WriteLine(c);
                            // Provide for exceptions.
                        }

                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex);
                    }
                }

                string message2 = "Driver Deleted";
                string caption2 = "Driver";
                MessageBoxResult result2 = MessageBox.Show(message2, caption2,
                                         MessageBoxButton.OK,
                                         MessageBoxImage.Exclamation);

            }
            else
            {
                string message = "Please select a driver";
                string caption = "Driver";
                MessageBoxResult result = MessageBox.Show(message, caption,
                                         MessageBoxButton.OK,
                                         MessageBoxImage.Exclamation);
            }


        }
       
    }
}
